package cn.school.stu.evaluation.service.impl;

import cn.school.classManage.dao.SchoolClassDao;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SecurityUtils;
import cn.school.common.utils.SysUserUtils;
import cn.school.stu.evaluation.dao.StuEvaDataDao;
import cn.school.stu.evaluation.dao.StuEvaIndicatorDao;
import cn.school.stu.evaluation.dao.StuEvaPlanDao;
import cn.school.stu.evaluation.dao.StuEvaTaskDao;
import cn.school.stu.evaluation.entity.StuEvaData;
import cn.school.stu.evaluation.entity.StuEvaIndicator;
import cn.school.stu.evaluation.entity.StuEvaPlan;
import cn.school.stu.evaluation.entity.StuEvaTask;
import cn.school.stu.evaluation.service.StuEvaTaskService;
import cn.school.sys.entity.SysUser;
import cn.school.sys.entity.SysUserRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: lx
 * @date: 2023/5/25 10:46
 * @description:
 */
@Service
public class StuEvaTaskServiceImpl extends ServiceImpl<StuEvaTaskDao, StuEvaTask> implements StuEvaTaskService {

    @Autowired
    StuEvaIndicatorDao stuEvaIndicatorDao;

    @Autowired
    StuEvaPlanDao stuEvaPlanDao;

    @Autowired
    StuEvaTaskDao stuEvaTaskDao;

    @Autowired
    StuEvaDataDao stuEvaDataDao;

    @Resource
    SchoolClassDao schoolClassDao;

    @Override
    @Transactional
    public Integer createStuEvaTask(StuEvaTask task) {
        Long classId = task.getClassId();
        SchoolClassVO schoolClassVO = schoolClassDao.selectById(classId);
        task.setTaskUserId(schoolClassVO.getClassTeacherId());
        task.setEvaStatus("0");
        int num = this.baseMapper.insert(task);
        if(num > 0){
            StuEvaPlan stuEvaPlan = stuEvaPlanDao.selectById(task.getStuEvaPlanId());
            stuEvaPlan.setState("1");
            int count = stuEvaPlanDao.updateById(stuEvaPlan);
            return count;
        }
        return -1;
    }

    @Override
    public IPage<StuEvaTask> findStuEvaTaskPageList(StuEvaTask vo) {
        SysUser sysUser = SysUserUtils.getSysUser();
//        Long userId = null;
        String taskUserId = null;
        //如果不是管理员则只能查当前登录人的数据
        List<SysUserRole> roleInfoList = SysUserUtils.getSysUser().getRoleInfoList();
        boolean isAdmin = false;
        for (SysUserRole sysUserRole : roleInfoList) {
            if("超级管理员".equals(sysUserRole.getRoleName())){
                isAdmin = true;
            }
        }
        if (!isAdmin){
            taskUserId = sysUser.getUsername();
        }
        Integer pageNum = vo.getPageNum();
        Integer pageSize = vo.getPageSize();
        int startNum = (pageNum - 1) * pageSize;
        String tenantId = sysUser.getTenantId();
        int count = stuEvaTaskDao.findStuEvaTaskCount(vo,tenantId,taskUserId);
        List<StuEvaTask> list = stuEvaTaskDao.findStuEvaTaskPageList(vo,startNum,tenantId,taskUserId);
        IPage<StuEvaTask> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> findStuEvaScoreTemplateByPlanId(Long planId) {
        //声明返回list
        List<Map<String, Object>> list = new ArrayList<>();

        StuEvaPlan stuEvaPlan = stuEvaPlanDao.selectById(planId);
        String[] strIndicatorIds = stuEvaPlan.getIndicators().split(",");//
        Integer[] intIndicatorIds = new Integer[strIndicatorIds.length];
        for (int i = 0; i < strIndicatorIds.length; i++) {
            intIndicatorIds[i] = Integer.parseInt(strIndicatorIds[i]);
        }
        List<Integer> indicatorIds = Arrays.asList(intIndicatorIds);
        for(Integer indicatorId : indicatorIds){
            StuEvaIndicator stuEvaIndicator = stuEvaIndicatorDao.selectById(indicatorId);
            //判断是否当前考评指标存在子考评项
            LambdaQueryWrapper<StuEvaIndicator> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StuEvaIndicator::getPid,indicatorId);
            List<StuEvaIndicator> childStuEvaIndicatorList = stuEvaIndicatorDao.selectList(wrapper);
            //不存在子考评项情况
            if(childStuEvaIndicatorList == null || childStuEvaIndicatorList.size() == 0){
                Map<String,Object> map = new HashMap<>();
                map.put("indicator",stuEvaIndicator.getIndicatorName());
                map.put("element","");
                map.put("remark",stuEvaIndicator.getRemark());
                map.put("grade","");
                list.add(map);
            }else {
                for(StuEvaIndicator item : childStuEvaIndicatorList){
                    Map<String,Object> map = new HashMap<>();
                    map.put("indicator",stuEvaIndicator.getIndicatorName());
                    map.put("element",item.getIndicatorName());
                    map.put("remark",item.getRemark());
                    map.put("grade","");
                    list.add(map);
                }
            }
        }
        return list;
    }

    @Override
    public List<StuEvaTask> findStuEvaByPlanId(Long planId) {
        return stuEvaTaskDao.findStuEvaByPlanId(planId);
    }

    @Override
    public int delTaskByTaskId(Long taskId) {
        int num = 0;
        LambdaQueryWrapper<StuEvaTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuEvaTask::getId,taskId);
        LambdaQueryWrapper<StuEvaData> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(StuEvaData::getTaskId, taskId);
        List<StuEvaData> stuEvaDataList = stuEvaDataDao.selectList(wrapper1);
        if (stuEvaDataList.size() != 0) {
            LambdaQueryWrapper<StuEvaData> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(StuEvaData::getTaskId,taskId);
            int delete = stuEvaDataDao.delete(wrapper2);
            if (delete > 0) {
                num = stuEvaTaskDao.delete(wrapper);
            }
        } else {
            num = stuEvaTaskDao.delete(wrapper);
        }
        return num;
    }
}
